package com.sport.facilities.booking.system.htmlchecker;

import java.util.ArrayList;
import java.util.List;

import org.htmlparser.Node;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

import android.util.Log;

import com.sport.facilities.booking.system.core.HtmlHelper;
import com.sport.facilities.booking.system.model.Booking;

public class ConfirmBookingPageChecker extends BasicChecker {

	private final String TAG = "CancelBookingPageChecker";

	public List<Booking> getConfirmBookingListfromHtml(String confirmBookingHtml)
			throws ParserException, Exception {

		super.hasError(confirmBookingHtml);
		// Get NodeList from HTML
		List<Booking> bookingList = new ArrayList<Booking>();
		NodeList nodelist = HtmlHelper.getNodeListByTag(confirmBookingHtml,
				"table");
		NodeList inputnodelist = HtmlHelper.getNodeListByTag(confirmBookingHtml,
				"div");

		// Log.d(TAG, "TABLENodes:" + nodelist.size());
		// Log.d(TAG, "DIVNodes:" + inputnodelist.size());

		TagNameFilter fontFilter = new TagNameFilter("FONT");
		HasAttributeFilter hasSizeAttribute = new HasAttributeFilter("size");

		TagNameFilter inputFilter = new TagNameFilter("INPUT");
		HasAttributeFilter hasValueAttribute = new HasAttributeFilter("value");

		AndFilter testFilter = new AndFilter(fontFilter, hasSizeAttribute);
		AndFilter testFilter2 = new AndFilter(inputFilter, hasValueAttribute);

		NodeList infofoundNodeList = nodelist.extractAllNodesThatMatch(
				testFilter, true);
		NodeList inputfoundNodeList = inputnodelist.extractAllNodesThatMatch(
				testFilter2, true);
		// Log.d(TAG, "TextNode:" + infofoundNodeList.size());
		// Log.d(TAG, "InputNode:" + inputfoundNodeList.size());

		if (infofoundNodeList.size() % 5 != 2) {
			/*
			 * We have five elements, so the record must should be divided by 5
			 * [Date, Time, Centre, Court, Activity]
			 */
			// [Date, Time, Centre, Court, Activity] Data elements miss match!
			// throw new Exception(
			// "No Booking data was found.");
			// bookingList = null;
		}

		/*
		 * The first five textNode are the Header We should skip the first 7
		 * results Result
		 */

		int j = 0;
		for (int i = 0; i < infofoundNodeList.size(); i++) {
			if (i <= 6 || i % 5 != 2 || i + 1 == infofoundNodeList.size()) {
				continue;
			}

			Booking booking = new Booking();

			String radioValue = inputfoundNodeList.elementAt(j).toHtml();
			radioValue = radioValue.replaceAll("\\D+", "");
			Node dateNode = infofoundNodeList.elementAt(i).getNextSibling();
			Node timeNode = infofoundNodeList.elementAt(i + 1).getNextSibling();
			Node centreNode = infofoundNodeList.elementAt(i + 2)
					.getNextSibling();
			Node courtNode = infofoundNodeList.elementAt(i + 3)
					.getNextSibling();
			Node activityNode = infofoundNodeList.elementAt(i + 4)
					.getNextSibling();

			Log.d(TAG, "RADIOVALUE:"
					+ inputfoundNodeList.elementAt(j).getNextSibling()
							.toString());

			if (i % 5 == 2) {
				j++;
			}
			booking.setId(radioValue);
			booking.setDate(dateNode.getText());
			booking.setTime(timeNode.getText());
			booking.setCourt(courtNode.getText());
			booking.setCentre(centreNode.getText());
			booking.setCourt(courtNode.getText());
			booking.setActivity(activityNode.getText());

			Log.d(TAG, "Booking : " + booking.toString());
			bookingList.add(booking);
		}

		return bookingList;
	}

}
